package hhm.android.main

import android.os.Bundle
import android.text.TextUtils
import androidx.core.content.ContextCompat
import androidx.databinding.DataBindingUtil
import com.trello.rxlifecycle4.RxLifecycle
import com.trello.rxlifecycle4.android.ActivityEvent
import hhm.android.library.datePicker.bean.DateParams
import hhm.android.library.utils.DialogUtils
import hhm.android.library.utils.GetDateListener
import hhm.android.library.utils.GetStringListener
import hhm.android.library.weightHeightPicker.footPicker.FootParams
import hhm.android.main.databinding.ActivityInputFootByHandBinding
import siau.android.base.HttpHelper
import siau.android.base.SBBaseActivity
import siau.android.http.model.RecordHeightDataRequest
import siau.android.http.model.RecordWeightDataRequest
import siau.android.library.KeyString
import java.text.SimpleDateFormat
import java.util.*

class InputFootByHandActivity : SBBaseActivity() {

    lateinit var dataBinding: ActivityInputFootByHandBinding
    private var childId: Int = -1
    private var timeChange = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setTitleWhiteAndTextBlank()
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_input_foot_by_hand)
        initTitleWithLine(getString(R.string.input_by_hand))
        dataBinding.lifecycleOwner = this

        childId = intent.getIntExtra(KeyString.childId, -1)
        var oldData = intent.getStringExtra(KeyString.data)
        if (oldData == "- -") {
            oldData = "0.0"
            dataBinding.activityInputFootByHandTv.text = "0.00"
        } else {
            dataBinding.activityInputFootByHandTv.text = oldData.toString()
        }

        val dateParams = FootParams()
        dateParams.types = IntArray(2)
        dateParams.types[0] = FootParams.TYPE_CM
        dateParams.types[1] = FootParams.TYPE_MM
        dataBinding.wheelLayout.show(oldData!!.toDouble(), dateParams, ContextCompat.getColor(this, R.color.mainTextColor))
        dataBinding.wheelLayout.setOnChangeListener(object : GetStringListener {
            override fun getString(s: String) {
                dataBinding.activityInputFootByHandTv.text = s
            }
        })

        val sdf = SimpleDateFormat("yyyy/MM/dd HH:mm")
        val time = sdf.format(Date())
        val times = time.split(" ")
        dataBinding.activityInputFootByHandDate.text = times[0]
        dataBinding.activityInputFootByHandTime.text = times[1]

        dataBinding.activityInputFootByHandSelectDate.setOnClickListener {
            DialogUtils.showDatePicBottomView(
                this,
                object : GetDateListener {
                    override fun getDate(date: Date, string: String) {
                        dataBinding.activityInputFootByHandDate.text = string
                    }
                },
                "yyyy/MM/dd",
                DateParams.TYPE_YEAR, DateParams.TYPE_MONTH, DateParams.TYPE_DAY,
            )
        }
        dataBinding.activityInputFootByHandSelectTime.setOnClickListener {
            DialogUtils.showDatePicBottomView(
                this, object : GetDateListener {
                    override fun getDate(date: Date, string: String) {
                        timeChange = true
                        dataBinding.activityInputFootByHandTime.text = string
                    }
                }, "HH:mm", DateParams.TYPE_HOUR, DateParams.TYPE_MINUTE
            )
        }
        dataBinding.activityInputFootByHandBtn.setOnClickListener {
            showLoadingFragment(R.id.activity_input_foot_by_hand_fl)
            val sdf = SimpleDateFormat("yyyy/MM/dd HH:mm:ss")
            val time = sdf.format(Date())
            val times = time.split(" ")
            val ss = times[1].split(":")
            var d = if (timeChange) {
                sdf.parse(dataBinding.activityInputFootByHandDate.text.toString() + " " + dataBinding.activityInputFootByHandTime.text.toString() + ":" + ss[2])
            } else {
                sdf.parse(dataBinding.activityInputFootByHandDate.text.toString() + " " + times[1])
            }
            val r = RecordHeightDataRequest(childId, d.time, 2, dataBinding.activityInputFootByHandTv.text.toString(), "CM")
            HttpHelper().recordFootSize(r)?.compose(RxLifecycle.bindUntilEvent(lifecycle(), ActivityEvent.STOP))?.subscribe({
                removeLoadingFragment()
                setResult(1001)
                finish()
            }, {
                removeLoadingFragment()
                showToast(it.message)
            })
        }
    }
}